package com.dkd.common.utils;

import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.List;
import java.util.Arrays;

public class JsonUtils {

    // 将 List 转换为 JSON 字符串
    public static String convertListToJson(List<Integer> list) {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            return objectMapper.writeValueAsString(list);  // 将 List 转换为 JSON 字符串
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    // 将数组转换为 JSON 字符串
    public static String convertArrayToJson(int[] array) {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            return objectMapper.writeValueAsString(array);  // 将数组转换为 JSON 字符串
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    // 示例：主方法，测试 JSON 转换
    public static void main(String[] args) {
        List<Integer> dataList = Arrays.asList(1, 2, 3, 4, 5);
        String jsonData = convertListToJson(dataList);
        System.out.println(jsonData);  // 输出：[1,2,3,4,5]

        int[] dataArray = {1, 2, 3, 4, 5};
        String jsonArrayData = convertArrayToJson(dataArray);
        System.out.println(jsonArrayData);  // 输出：[1,2,3,4,5]
    }
}

